#!/bin/sh
#
# $Id: postinst 60 2011-12-08 21:29:22Z imooreyahoo@gmail.com $
#
# see: dh_installdeb(1)

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

case "$1" in
    configure)

	# Check for XML config
	if ! omv_config_exists "//services/website"; then
		omv_config_add_element "//services" "website" ""
	fi
	
    if ! omv_config_exists "//services/website/enable"; then
        omv_config_add_element "//services/website" "enable" "0"
    fi

	
	# Fix permissions
	omv_install_fixperms
	
	# Add php.ini file?
	if [ ! -e /var/www/omv-website-vhost/php.ini ]; then
		cp /var/www/openmediavault/cgi/php.ini /var/www/omv-website-vhost/php.ini
	fi
	
	# Fix permissions
	chmod 755 /var/www/omv-website-vhost
	chmod 644 /var/www/omv-website-vhost/php.ini
	chmod 744 /var/www/omv-website-vhost/php-fcgi
	
	# Fix ownership?
	if [ "$(omv_config_get "//services/website/enable")" = "1" ]; then
	
		# CGI user
		CGIUSER=""
		CGIGRP=""
		USERUUID="$(omv_config_get "//services/website/cgiuser")"
		if [ "${USERUUID}" != "" ]; then
		
			CGIUSER=$(omv_config_get "//system/usermanagement/users/user[uuid='${USERUUID}']/name")
			CGIGRP=$(groups "${CGIUSER}" | awk '{ print $3 }')
		
			if [ "${CGIUSER}" != "" ] && [ "${CGIGRP}" != "" ]; then
				chown -R ${CGIUSER}:${CGIGRP} /var/www/omv-website-vhost
			fi
		
		fi

	
	fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
